package com.chhh.cranelease.config;

import com.chhh.cranelease.exception.UseNotLoginException;
import com.chhh.cranelease.model.UserWithRole;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
    拦截器
 */
@Component
public class LoginHandlerInterrupt implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserWithRole user = (UserWithRole) request.getSession().getAttribute("user");
        if (user != null && user.getR_id()==0){
            // 已登录
            return true;
        }else {
            request.getRequestDispatcher("/logout").forward(request,response);
            return false;
        }
    }
}
